<?php
 

define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hd_settings.inc.php');
require_once('language/'.$hd_settings['language'].'.inc.php');
require_once('inc/common.inc.php');

/* Print header */
require_once('inc/header.inc.php');

$trackingID=strtoupper(hd_input($_GET['track']));
if (empty($trackingID)) {print_form();}

/* Connect to database */
require_once('inc/database.inc.php');
hd_dbConnect() or hd_error("$hdlang[cant_connect_db] $hdlang[contact_webmsater] $hd_settings[webmaster_mail]!");

/* Get request info */
$sql = "SELECT * FROM `hd_requests` WHERE `requestid`='$trackingID' LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
if (hd_dbNumRows($result) != 1) {hd_error($hdlang['request_not_found']);}
$request = hd_dbFetchAssoc($result);

/* Get residence name and ID */
$sql = "SELECT * FROM `hd_residences` WHERE `id`=$request[residence] LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
/* If this residence has been deleted use the default residence with ID 1 */
    if (hd_dbNumRows($result) != 1)
    {
        $sql = "SELECT * FROM `hd_residences` WHERE `id`=1 LIMIT 1";
        $result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
    }
$residence = hd_dbFetchAssoc($result);

/* Get replies */
$sql = "SELECT * FROM `hd_replies` WHERE `replyto`='$request[id]' ORDER BY `id` ASC";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$replies = hd_dbNumRows($result);
?>

<p class="smaller"><a href="<?php echo $hd_settings['site_url']; ?>"
class="smaller"><?php echo $hd_settings['site_title']; ?></a> &gt;
<a href="index.php?a=start" class="smaller"><?php echo $hd_settings['hd_title']; ?></a>
&gt; <?php echo $hdlang['your_request']; ?><br>&nbsp;</p>
</td>
</tr>
<tr>
<td>

<h3 align="center"><?php echo $request['subject']; ?></h3>



<div align="center">
<center>
<table border="0" width="750" cellspacing="1" cellpadding="5" class="white">
<tr>
<td>

<table border="0" cellspacing="1" cellpadding="1">
<?php
echo '
<tr>
<td>'.$hdlang['trackID'].': </td>
<td>'.$trackingID.'</td>
</tr>
<tr class="white">
<td class="white">'.$hdlang['request_status'].': </td>
<td class="white">';
$random=rand(10000,99999);

    switch ($request['status']) {
    case 0:
        echo '<font class="open">'.$hdlang['open'].'</font> [<a
        href="change_status.php?track='.$trackingID.'&s=3&Refresh='.$random.'">'.$hdlang['close_action'].'</a>]';
        break;
    case 1:
        echo '<font class="replied">'.$hdlang['wait_staff_reply'].'</font> [<a
        href="change_status.php?track='.$trackingID.'&s=3&Refresh='.$random.'">'.$hdlang['close_action'].'</a>]';
        break;
    case 2:
        echo '<font class="waitingreply">'.$hdlang['wait_cust_reply'].'</font> [<a
        href="change_status.php?track='.$trackingID.'&s=3&Refresh='.$random.'">'.$hdlang['close_action'].'</a>]';
        break;
    default:
        echo '<font class="resolved">'.$hdlang['closed'].'</font> [<a
        href="change_status.php?track='.$trackingID.'&s=1&Refresh='.$random.'">'.$hdlang['open_action'].'</a>]';
    }

echo '</td>
</tr>
<tr>
<td>'.$hdlang['created_on'].': </td>
<td>'.$request['dt'].'</td>
</tr>
<tr class="white">
<td class="white">'.$hdlang['last_update'].': </td>
<td class="white">'.$request['lastchange'].'</td>
</tr>
<tr>
<td>'.$hdlang['last_replier'].': </td>
<td>';
        if ($request['lastreplier']) {echo $hdlang['staff'];}
        else {echo $hdlang['Student'];}
echo '</td>
</tr>
<tr class="white">
<td class="white">'.$hdlang['residence'].': </td>
<td class="white">'.$residence['name'].'</td>
</tr>
<tr>
<td>'.$hdlang['replies'].': </td>
<td>'.$replies.'</td>
</tr>
<tr class="white">
<td class="white">'.$hdlang['priority'].': </td>
<td class="white">';
        if ($request['priority']==1) {echo '<font class="important">'.$hdlang['high'].'</font>';}
        elseif ($request['priority']==2) {echo '<font class="medium">'.$hdlang['medium'].'</font>';}
        else {echo $hdlang['low'];}
?>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><a href="print.php?track=<?php echo $trackingID; ?>" target="_blank"><?php echo $hdlang['printer_friendly']; ?></a></td>
</tr>
</table>

</td>
</tr>
<tr>
<td class="white">
    <table border="0" cellspacing="1">
    <tr>
    <td class="white"><?php echo $hdlang['date']; ?>:</td>
    <td class="white"><?php echo $request['dt']; ?></td>
    </tr>
    <tr>
    <td class="white"><?php echo $hdlang['name']; ?>:</td>
    <td class="white"><?php echo $request['name']; ?></td>
    </tr>
    <tr>
    <td class="white"><?php echo $hdlang['email']; ?>:</td>
    <td class="white"><a href="mailto:<?php echo $request['email']; ?>"><?php echo $request['email']; ?></a></td>
    </tr>
    <tr>
    <td class="white"><?php echo $hdlang['ip']; ?>:</td>
    <td class="white"><?php echo $request['ip']; ?></td>
    </tr>
    </table>

<?php
/* custom fields */
if ($hd_settings['use_custom']) {

    $myclass='class="white"';
    echo '<table border="0" cellspacing="1">';

    foreach ($hd_settings['custom_fields'] as $k=>$v) {
        if ($v['use']) {
            if ($myclass) {$myclass='';}
            else {$myclass='class="white"';}
echo <<<EOC
    <tr>
    <td $myclass>$v[name]:</td>
    <td $myclass>$request[$k]</td>
    </tr>

EOC;
        }
    }

    echo '</table>';
}

?>

<p><b><?php echo $hdlang['message']; ?>:</b><p>
<p><?php echo $request['message']; ?><br>&nbsp;</p>

<?php
if ($hd_settings['attachments']['use'] && !empty($request['attachments'])) {
    echo '<p><b>'.$hdlang['attachments'].':</b><br>';
    $att=explode(',',substr($request['attachments'], 0, -1));
    foreach ($att as $myatt) {
        list($att_id, $att_name) = explode('#', $myatt);
        echo '<img src="img/clip.gif" width="20" height="20"><a href="download_attachment.php?att_id='.$att_id.'">'.$att_name.'</a><br>';
    }
    echo '</p>';
}
?>

</td>
</tr>

<?php
$i=1;
while ($reply = hd_dbFetchAssoc($result))
{
if ($i) {$color=''; $i=0;}
else {$color='class="white"'; $i=1;}
echo <<<EOC
    <tr>
    <td $color>
        <table border="0" cellspacing="1">
        <tr>
        <td $color>$hdlang[date]:</td>
        <td $color>$reply[dt]</td>
        </tr>
        <tr>
        <td $color>$hdlang[name]:</td>
        <td $color>$reply[name]</td>
        </tr>
        </table>
    <p><b>$hdlang[message]:</b></p>
    <p>$reply[message]</p>

EOC;

if ($hd_settings['attachments']['use'] && !empty($reply['attachments'])) {
    echo '<p><b>'.$hdlang['attachments'].':</b><br>';
    $att=explode(',',substr($reply['attachments'], 0, -1));
    foreach ($att as $myatt) {
        list($att_id, $att_name) = explode('#', $myatt);
        echo '<img src="img/clip.gif" width="20" height="20"><a href="download_attachment.php?att_id='.$att_id.'">'.$att_name.'</a><br>';
    }
    echo '</p>';
}

echo '</td></tr>';
}
?>
</table>
</center>
</div>

<hr width="750">

<h3 align="center"><?php echo $hdlang['add_reply']; ?></h3>

<form method="POST" action="reply_request.php" enctype="multipart/form-data">
<p align="center"><?php echo $hdlang['message']; ?>: <font class="important">*</font><br>
<textarea name="message" rows="12" cols="60"></textarea></p>

<?php
/* attachments */
if ($hd_settings['attachments']['use']) {

?>

<p align="center">

<?php
echo $hdlang['attachments'].':<br>';
    for ($i=1;$i<=$hd_settings['attachments']['max_number'];$i++) {
        echo '<input type="file" name="attachment['.$i.']" size="50"><br>';
    }
?>

<?php echo$hdlang['accepted_types']; ?>: <?php echo '*'.implode(', *', $hd_settings['attachments']['allowed_types']); ?><br>
<?php echo $hdlang['max_file_size']; ?>: <?php echo $hd_settings['attachments']['max_size']; ?> Kb
(<?php echo sprintf("%01.2f",($hd_settings['attachments']['max_size']/1024)); ?> Mb)

</p>

<?php
}
?>

<p align="center"><input type="hidden" name="orig_id" value="<?php echo $request['id']; ?>">
<input type="hidden" name="orig_name" value="<?php echo $request['name']; ?>">
<input type="hidden" name="orig_track" value="<?php echo $trackingID; ?>">
<input type="submit" value="<?php echo $hdlang['submit_reply']; ?>" class="button"></p>

</form>

<?php
require_once('inc/footer.inc.php');

/*** START FUNCTIONS ***/

function print_form() {
global $hd_settings, $hdlang;
?>
<p class="smaller"><a href="<?php echo $hd_settings['site_url']; ?>"
class="smaller"><?php echo $hd_settings['site_title']; ?></a> &gt;
<a href="index.php?a=start" class="smaller"><?php echo $hd_settings['hd_title']; ?></a>
&gt; <?php echo $hdlang['view_request']; ?><br>&nbsp;</p>
</td>
</tr>
<tr>
<td>

<p>&nbsp;</p>
<h3 align="center"><?php echo $hdlang['view_request']; ?></h3>
<p>&nbsp;</p>

<form action="request.php" method="GET">
<div align="center">
<center>
<table class="white" cellspacing="1" cellpadding="8">
<tr>
<td class="white">
<p align="center"><?php echo $hdlang['request_trackID']; ?>: <input type="text" name="track" maxlength="10"
size="12"></p>
<p align="center"><input type="hidden" name="Refresh" value="<?php echo rand(10000,99999); ?>">
<input type="submit" value="<?php echo $hdlang['view_request']; ?>" class="button"></p>
</td>
</tr>
</table>
</center>
</div>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php
require_once('inc/footer.inc.php');
exit();
} // End print_form()

?>
